package com.lfy.modules.disk.controller;

import com.lfy.aop.anno.CheckFile;
import com.lfy.modules.common.anno.AutoUser;
import com.lfy.modules.common.anno.NoNeedLogin;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.domain.entity.UserEntity;
import com.lfy.modules.disk.domain.dto.*;
import com.lfy.modules.common.domain.FileUrlVO;
import com.lfy.modules.disk.domain.vo.FileVO;
import com.lfy.modules.disk.domain.vo.ShareFilePwdVO;
import com.lfy.modules.disk.service.DiskService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/admin/disk")
@RestController
public class DiskController {
    @Resource
    private DiskService diskService;

    @PostMapping("/upload/small")
    @CheckFile
    public R<Void> uploadSmallFile(@RequestParam("file") MultipartFile file, @RequestParam("parentId") Long parentId,
                                   @RequestParam("type") Integer type, @AutoUser UserEntity user) {
        return diskService.uploadSmallFile(file, user, parentId, type);
    }

    @RequestMapping("/test")
    @NoNeedLogin
    public R<String> test() {
        diskService.deleteChunkFile();
        return R.success("test");
    }

    /**
     * 创建文件夹
     */
    @PostMapping("/create/folder")
    public R<Void> createFolder(@RequestBody @Validated CreateFolderDTO dto, @AutoUser UserEntity user) {
        return diskService.createFolder(dto, user);
    }

    /**
     * 获取文件列表
     */
    @GetMapping("/list")
    public R<List<FileVO>> list(@RequestParam("parentId") Long parentId, @AutoUser UserEntity user) {
        return this.diskService.getFileListByParentId(parentId, user);
    }

    /**
     * 文件重命名
     */

    @PostMapping("/rename")
    public R<Void> rename(@RequestBody @Validated ReNameDTO dto, @AutoUser UserEntity user) {
        return diskService.rename(dto, user);
    }

    /**
     * 删除文件
     */
    @PostMapping("/delete/{id}")
    public R<Void> delete(@PathVariable("id") Long id, @AutoUser UserEntity user) {
        return diskService.deleteFile(id, user);
    }

    /**
     * 秒传
     * 判断文件是否存在
     */
    @GetMapping("/exist")
    public R<Boolean> exist(@RequestParam("md5") String md5, @RequestParam("fileName") String fileName, @RequestParam("parentId") Long parentId) {
        try {
            return diskService.exist(md5, fileName, parentId);
        } catch (Exception e) {
            return R.success(false);
        }

    }

    /**
     * 分片
     * 校验分片是否存在
     */

    @GetMapping("/check/chunk")
    public R<Boolean> checkChunk(@RequestParam("md5") String md5, @RequestParam("chunk") Integer chunk) {
        return diskService.checkChunk(md5, chunk);
    }

    /**
     * 分片
     * 上传分片
     */
    @PostMapping("/upload/chunk")
    public R<Void> uploadChunk(@RequestParam("file") MultipartFile file, @RequestParam("md5") String md5,
                               @RequestParam("chunk") Integer chunk, @AutoUser UserEntity user) {
        return diskService.uploadChunk(file, md5, chunk, user);
    }

    /**
     * 合并分片
     */
    @PostMapping("/merge")
    public R<Void> merge(@RequestBody @Validated MergeFileDTO dto, @AutoUser UserEntity user) {
        return diskService.merge(dto.getMd5(), dto.getFileName(), dto.getParentId(), user, dto.getCutSize(), dto.getType(),dto.getFileSize());
    }
/*    *//**
     * 下载文件
     *//*
    @GetMapping("/download/{id}")
    public ResponseEntity<?> download(@PathVariable("id") Long id, @AutoUser UserEntity user, HttpServletResponse response) {
        return diskService.download(id, user,response);
    }   */
    /**
     * 下载文件
     */
    @GetMapping("/download/{id}")
    @NoNeedLogin
    public void download(@PathVariable("id") Long id, @AutoUser UserEntity user, HttpServletResponse response) {
       diskService.downloadToStream(id, user,response);
    }
    /**
     * 粘贴文件
     * 修改文件ParentId
     */

    @PostMapping("/paste")
    public R<Void> paste(@RequestBody @Validated PasteFileDTO dto, @AutoUser UserEntity user) {
        return diskService.paste(dto.getId(),dto.getParentId(), user);
    }

    /**
     * 分享文件
     * @param shareDTO 分享DTO
     * @param user 用户信息
     * @return 分享文件链接信息
     */
    @PostMapping("/create/share")
    public R<ShareFilePwdVO> share(@RequestBody @Validated ShareDTO shareDTO, @AutoUser UserEntity user){
            return diskService.createShareFile(shareDTO,user);
    }

}
